package com.mbyte.easy.acpect;

import com.mbyte.easy.admin.entity.DraftLog;
import com.mbyte.easy.admin.service.IDraftLogService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.util.Utility;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;


/**
 * @ClassName DraftLogAspect
 * @Description 稿件日志切面
 * @Author lxt
 * @Date 2020/3/5 17:39
 */
@Aspect
@Component
@Slf4j
public class DraftLogAspect extends BaseController {

    @Autowired
    private IDraftLogService draftLogService;

    @Pointcut("@annotation(com.mbyte.easy.acpect.DraftLogRequired)")
    public void draftLog(){}

    /**
     * 记录
     * @param joinPoint
     * @throws Throwable
     */
    @Around("draftLog()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取所调用方法信息
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature)signature;
        Method targetMethod = methodSignature.getMethod();
        String targetMethodName = joinPoint.getTarget().getClass().getName()+"."+targetMethod.getName();
        // 获取注解
        DraftLogRequired draftLogRequired = getDraftLog(joinPoint);
        if(draftLogRequired == null) {
            log.error("获取方法【{}】上注解【DraftLog】失败！",targetMethodName);
        }
        // 需要记录
        if(draftLogRequired.required()){
            // 实际的记录日志操作
            saveDraftLog(draftLogRequired,methodSignature,joinPoint);
        }
        return joinPoint.proceed();
    }

    /**
     * @Title: getDraftLog
     * @Description: 获取方法上注解
     * @Author: lxt
     * @param: joinPoint
     * @Date: 2019-10-28 11:02
     * @return: com.mbyte.easy.annotation.UserReferenceRequired
     * @throws:
     */
    private DraftLogRequired getDraftLog(ProceedingJoinPoint joinPoint) throws Exception {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
        return realMethod.getAnnotation(DraftLogRequired.class);
    }


    /**
     * @Title: saveDraftLog
     * @Description: 存储用户浏览记录
     * @Author: lxt
     * @param: userReferenceRequired
     * @param: methodSignature
     * @param: joinPoint
     * @Date: 2019-10-28 15:44
     * @throws:
     */
    private void saveDraftLog(DraftLogRequired draftLogRequired, MethodSignature methodSignature
            , ProceedingJoinPoint joinPoint){
        // 稿件日志
        DraftLog draftLog = new DraftLog()
                .setUserId(Utility.getSysUser().getId())
                .setCreateTime(LocalDateTime.now());
        //获取方法参数名
        String[] paramNames = methodSignature.getParameterNames();
        //获取参数值
        Object[] paramValues = joinPoint.getArgs();
        for (int i = 0; i < paramNames.length; i++) {
            if(paramValues[i] == null){
                continue;
            }
            if(draftLogRequired.draftId().equals(paramNames[i])){
                String paramValue = paramValues[i].toString();
                draftLog.setDraftId(StringUtils.isNoneBlank(paramValue) ? Long.parseLong(paramValue) : null);
            }else if(draftLogRequired.reason().equals(paramNames[i])){
                draftLog.setReason(paramValues[i].toString());
            }else if(draftLogRequired.actionContent().equals(paramNames[i])){
                draftLog.setActionContent(paramValues[i].toString());
            }else if(draftLogRequired.actionType().equals(paramNames[i])){
                draftLog.setActionType(paramValues[i].toString());
            }else if(draftLogRequired.afterState().equals(paramNames[i])){
                String paramValue = paramValues[i].toString();
                draftLog.setAfterState(StringUtils.isNoneBlank(paramValue) ? Integer.parseInt(paramValue) : null);
            }else if(draftLogRequired.primaryState().equals(paramNames[i])){
                String paramValue = paramValues[i].toString();
                draftLog.setPrimaryState(StringUtils.isNoneBlank(paramValue) ? Integer.parseInt(paramValue) : null);
            }else if(draftLogRequired.groupId().equals(paramNames[i])){
                draftLog.setGroupId(paramValues[i].toString());
            }else{
                log.error("未知字段【{}】",paramNames[i]);
            }
        }
        draftLogService.save(draftLog);
    }
}


















